package View;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import Bussiness.Search_Action;

@SuppressWarnings("serial")
public class QuenMatKhau extends JFrame {

	private JPanel contentPane;
	private JTextField txtTaiKhoan;
	private JTextField txtTen;


	/**
	 * Create the frame.
	 */
	public QuenMatKhau() {
		super("Quên mật khẩu ...");
		setIconImage(Toolkit.getDefaultToolkit().getImage(QuenMatKhau.class.getResource("/Icon/QLSV.jpg")));
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 314, 165);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.activeCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Tài khoản:");
		lblNewLabel.setBounds(10, 58, 73, 14);
		contentPane.add(lblNewLabel);
		
		txtTaiKhoan = new JTextField();
		txtTaiKhoan.setBounds(111, 55, 186, 20);
		contentPane.add(txtTaiKhoan);
		txtTaiKhoan.setColumns(10);
		
		JLabel lblTn = new JLabel("Tên xát nhân:");
		lblTn.setBounds(10, 104, 73, 14);
		contentPane.add(lblTn);
		
		txtTen = new JTextField();
		txtTen.setBounds(111, 101, 186, 20);
		contentPane.add(txtTen);
		txtTen.setColumns(10);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBackground(SystemColor.inactiveCaption);
		toolBar.setFloatable(false);
		toolBar.setBounds(0, 0, 444, 31);
		contentPane.add(toolBar);
		
		JButton btnXacNhan = new JButton("Xác nhận");
		btnXacNhan.setBackground(new java.awt.Color(227, 239, 249));
		btnXacNhan.setIcon(new ImageIcon(QuenMatKhau.class.getResource("/Icon/Sua.png")));
		toolBar.add(btnXacNhan);
		
		JLabel label = new JLabel();
		label.setText(" | ");
		label.setForeground(new Color(219, 205, 205));
		toolBar.add(label);
		
		JButton btnNhapLai = new JButton("Nhập lại");
		btnNhapLai.setBackground(new java.awt.Color(227, 239, 249));
		btnNhapLai.setIcon(new ImageIcon(QuenMatKhau.class.getResource("/Icon/Reset.png")));
		toolBar.add(btnNhapLai);
		btnNhapLai.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nhaplai();
			}
		});
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 42, 444, 2);
		contentPane.add(separator);
		btnXacNhan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, xacnhan());
				if(xacnhan().indexOf("Mật khẩu của bạn là") == 0){
					dongForm();
				}
			}
		});
		pack();
	}


	protected void dongForm() {
		this.dispose();
	}

	/**
	 * Reset form
	 */
	protected void nhaplai() {
		txtTaiKhoan.setText("");
		txtTen.setText("");
	}


	/**
	 * Submit to server to confirm account
	 */
	protected String xacnhan() {
		String thongBao = null;
		String taiKhoan = txtTaiKhoan.getText().trim();
		String tenXacNhan = txtTen.getText().trim();
		
		if("".equalsIgnoreCase(taiKhoan) && "".equalsIgnoreCase(tenXacNhan)){
			thongBao = "Bạn chưa nhập dữ liệu!";
		}else if("".equalsIgnoreCase(taiKhoan)){
			thongBao = "Bạn chưa nhập tên đăng nhập!";
		}else if("".equalsIgnoreCase(tenXacNhan)){
			thongBao = "Bạn chưa nhập tên xác nhận!";
		}else{
			Search_Action sa = new Search_Action();
			System.out.println(sa.XacNhanTaiKhoan(taiKhoan, tenXacNhan));
			return sa.XacNhanTaiKhoan(taiKhoan, tenXacNhan);
		}
		return thongBao; 
	}
}
